package com.wandian.net
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;

	public class SWFAssetLoader extends EventDispatcher
	{
		private var fLoadedContent_mc:MovieClip;
		protected var fLoadedBytes_int:int=0;
		private var fLoader_ldr:Loader;
		private var fApplicationDomain_ad:ApplicationDomain;
		protected var fTotalBytes_int:int=0;
		public static const EVENT_LOADING_COMPLETED:String="onLoadingCompleted";
		public static const EVENT_LOADING_PROGRESS_CHANGED:String="onLoadingProgressChanged";
		public static const EVENT_LOADING_ERROR_OCCURED:String="onLoadingErrorOccured";

		public function SWFAssetLoader()
		{
			fLoadedBytes_int=0;
			fTotalBytes_int=0;
			return;
		} // end function

		public function get library():ApplicationDomain
		{
			if (!this.isLoaded)
			{
				return this.fApplicationDomain_ad;
			}
			return this.loader.contentLoaderInfo.applicationDomain;
		} // end function

		protected function generateLoadingCompletedEvent():void
		{
			this.dispatchEvent(new Event(EVENT_LOADING_COMPLETED));
			return;
		} // end function

		public function startLoading(param1:String):void
		{
			var request:URLRequest;
			var lContext_lc:LoaderContext;
			var aSwfUrl_str:*=param1;
			if (this.isLoaded || this.loadingInitialized)
			{
				return;
			}
			this.fLoader_ldr=new Loader();
			this.fLoader_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadingCompleted);
			this.fLoader_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onLoadingProgressChanged);
			this.fLoader_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onLoadingErrorOccured);
			request=new URLRequest(aSwfUrl_str);
			try
			{
				lContext_lc=new LoaderContext(false, this.library);
				this.fLoader_ldr.load(request, lContext_lc);
			}
			catch (error:Error)
			{
				this.onLoadingErrorOccured();
			}
			return;
		} // end function

		protected function generateLoadingProgressChangedEvent():void
		{
			this.dispatchEvent(new Event(EVENT_LOADING_PROGRESS_CHANGED));
			return;
		} // end function

		protected function get loader():Loader
		{
			return this.fLoader_ldr;
		} // end function

		public function get isLoaded():Boolean
		{
			return Boolean(this.content);
		} // end function

		public function get bytesLoaded():int
		{
			return this.fLoadedBytes_int;
		} // end function

		public function get bytesTotal():int
		{
			return this.fTotalBytes_int;
		} // end function

		public function get loadingInProgress():Boolean
		{
			return this.loadingInitialized && !this.isLoaded;
		} // end function

		protected function onLoadingErrorOccured(event:Event=null):void
		{
			this.dispatchEvent(new Event(EVENT_LOADING_ERROR_OCCURED));
			return;
		} // end function

		protected function onLoadingProgressChanged(event:ProgressEvent):void
		{
			this.fLoadedBytes_int=ProgressEvent(event).bytesLoaded;
			this.fTotalBytes_int=ProgressEvent(event).bytesTotal;
			this.generateLoadingProgressChangedEvent();
			return;
		} // end function

		protected function onLoadingCompleted(event:Event):void
		{
			this.fLoadedBytes_int=this.fTotalBytes_int;
			this.fLoadedContent_mc=MovieClip(LoaderInfo(event.target).content);
			this.generateLoadingCompletedEvent();
			return;
		} // end function

		public function set library(param1:ApplicationDomain)
		{
			this.fApplicationDomain_ad=param1;
			return;
		} // end function

		public function get loadingInitialized():Boolean
		{
			return Boolean(this.loader);
		} // end function

		public function get content():MovieClip
		{
			return this.fLoadedContent_mc;
		} // end function

		public function get loadingProgressInPercents():Number
		{
			if (this.bytesTotal == 0)
			{
				return 0;
			}
			return 100 * this.bytesLoaded / this.bytesTotal;
		} // end function

	}
}
